package com.moke.util.base
{

	public class ArrayUtilities
	{
		/** 从数组中随机出一部分数据  */
		public static function randomize(aArray:Array):Array
		{
			var aCopy:Array = aArray.concat();
			var aRandomized:Array = new Array();

			var oElement:Object;
			var nRandom:Number;
			for (var i:Number = 0; i < aCopy.length; i++)
			{
				nRandom = NumberUtilities.random(0, aCopy.length - 1);
				aRandomized.push(aCopy[nRandom]);
				aCopy.splice(nRandom, 1);
				i--;
			}
			return aRandomized;
		}

		/** 将数组中的所有 Number 类型的值相加 返回平均值 */
		public static function average(aArray:Array):Number
		{
			return sum(aArray) / aArray.length;
		}

		/** 将数组中的所有 Number 类型的值相加 返回 */
		public static function sum(aArray:Array):Number
		{
			var nSum:Number = 0;
			for (var i:Number = 0; i < aArray.length; i++)
			{
				if (typeof aArray[i] == "number")
				{
					nSum += aArray[i];
				}
			}
			return nSum;
		}

		/** 取出数组中最大的数字元素  */
		public static function max(aArray:Array):Number
		{
			var aCopy:Array = aArray.concat();
			aCopy.sort(Array.NUMERIC);
			var nMaximum:Number = Number(aCopy.pop());
			return nMaximum;
		}

		/** 取出数组中最小的数字元素  */
		public static function min(aArray:Array):Number
		{
			var aCopy:Array = aArray.concat();
			aCopy.sort(Array.NUMERIC);
			var nMinimum:Number = Number(aCopy.shift());
			return nMinimum;
		}

		/**  交换数组中 某两个下标位置的数据 */
		public static function switchElements(aArray:Array, nIndexA:Number, nIndexB:Number):void
		{
			var oElementA:Object = aArray[nIndexA];
			var oElementB:Object = aArray[nIndexB];
			aArray.splice(nIndexA, 1, oElementB);
			aArray.splice(nIndexB, 1, oElementA);
		}

		/** 比较两个对象 */
		private static function objectEquals(oInstanceA:Object, oInstanceB:Object):Boolean
		{
			for (var sItem:String in oInstanceA)
			{
				if (oInstanceA[sItem] is Object)
				{
					if (!objectEquals(oInstanceA[sItem], oInstanceB[sItem]))
					{
						return false;
					}
				}
				else
				{
					if (oInstanceA[sItem] != oInstanceB[sItem])
					{
						return false;
					}
				}
			}
			return true;
		}

		/**比较两个数组内容*/
		public static function equals(aArrayA:Array, aArrayB:Array, bNotOrdered:Boolean, bRecursive:Boolean):Boolean
		{
			if (aArrayA.length != aArrayB.length)
			{
				return false;
			}
			var aArrayACopy:Array = aArrayA.concat();
			var aArrayBCopy:Array = aArrayB.concat();
			if (bNotOrdered)
			{
				aArrayACopy.sort();
				aArrayBCopy.sort();
			}
			for (var i:Number = 0; i < aArrayACopy.length; i++)
			{
				if (aArrayACopy[i] is Array && bRecursive)
				{
					if (!equals(aArrayACopy[i], aArrayBCopy[i], bNotOrdered, bRecursive))
					{
						return false;
					}
				}
				else if (aArrayACopy[i] is Object && bRecursive)
				{
					if (!objectEquals(aArrayACopy[i], aArrayBCopy[i]))
					{
						return false;
					}
				}
				else if (aArrayACopy[i] != aArrayBCopy[i])
				{
					return false;
				}
			}
			return true;
		}
	}

}
